/**
 * 
 */
package com.strugglerz.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.strugglerz.biz.service.ISysAuthService;
import com.strugglerz.dal.mapper.SysResourceMapper;
import com.strugglerz.dal.mapper.SysRoleMapper;
import com.strugglerz.dal.mapper.SysUserRoleMapper;
import com.strugglerz.model.dalentity.SysResource;
import com.strugglerz.model.dalentity.SysRole;
import com.strugglerz.model.dalentity.SysRoleCriteria;
import com.strugglerz.model.dalentity.SysUser;
import com.strugglerz.model.dalentity.SysUserRole;
import com.strugglerz.model.dalentity.SysUserRoleCriteria;
import com.strugglerz.model.exception.BizHandleException;


/**
 * @author suan
 */
@Service
public class SysAuthServiceImpl implements ISysAuthService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysResourceMapper sysResourceMapper;

    @Override
    @Transactional(rollbackFor = { BizHandleException.class })
    public SysUserRole insertAuthorization(SysUser user, SysRole role) throws BizHandleException {
        if (user == null || role == null || user.getId() < 1 || role.getId() < 1) {
            throw new BizHandleException("用户授权失败，数据不正确！");
        }

        SysUserRole userRole = new SysUserRole();
        userRole.setCreateTime(new Date());
        userRole.setUsrId(user.getId());
        userRole.setRoleId(role.getId());
        userRole.setCreateTime(new Date());

        int usrRoleId = sysUserRoleMapper.insert(userRole);
        if (usrRoleId < 1) {
            throw new BizHandleException("用户授权失败，未知异常！");
        }

        return userRole;
    }

    @Override
    @Transactional(rollbackFor = { BizHandleException.class })
    public boolean deleteAuthorization(SysUser user, List<SysRole> roles) throws BizHandleException {
        if (user == null || user.getId() < 1 || CollectionUtils.isEmpty(roles)) {
            throw new BizHandleException("删除用户授权失败，数据不正确！");
        }

        List<Integer> roleIds = new ArrayList<Integer>();
        for (SysRole role : roles) {
            if (role != null && role.getId() > 0) {
                roleIds.add(role.getId());
            }
        }
        SysUserRoleCriteria example = new SysUserRoleCriteria();
        example.createCriteria().andUsrIdEqualTo(user.getId()).andRoleIdIn(roleIds);

        return sysUserRoleMapper.deleteByExample(example) > 0;
    }

    @Override
    @Transactional(rollbackFor = { BizHandleException.class }, readOnly = true)
    public List<SysRole> queryUserRoles(Integer userId) throws BizHandleException {
        if (userId == null || userId < 1) {
            throw new BizHandleException("查询用户授权失败，数据不正确！");
        }
        SysUserRoleCriteria example = new SysUserRoleCriteria();
        example.createCriteria().andUsrIdEqualTo(userId);
        List<SysUserRole> usrRoles = sysUserRoleMapper.selectByExample(example);
        List<Integer> roleIds = null;
        List<SysRole> sysRoles = null;
        if (!CollectionUtils.isEmpty(usrRoles)) {
            roleIds = new ArrayList<Integer>();
            for (SysUserRole uRole : usrRoles) {
                if (uRole != null && uRole.getRoleId() > 0) {
                    roleIds.add(uRole.getRoleId());
                }
            }
            SysRoleCriteria sysRoleCriteria = new SysRoleCriteria();
            sysRoleCriteria.createCriteria().andIdIn(roleIds);
            sysRoles = sysRoleMapper.selectByExample(sysRoleCriteria);
        }

        return sysRoles;
    }

    @Override
    public List<SysResource> queryPermissions(List<Integer> roleIds) throws BizHandleException {
        if (CollectionUtils.isEmpty(roleIds)) {
            return null;
        }
        return sysResourceMapper.selectPermissions(roleIds);
    }

    @Override
    public List<String> queryAllRoleCodes() {
        return sysRoleMapper.selectAllRoleCodes();
    }

    @Override
    public List<String> queryAllPermissions() {
        return sysResourceMapper.selectAllPermissions();
    }

}
